package com.dldata.devtools30.controller.devtexcel;

import com.dldata.devtools30.entity.DevtExcelEntity;
import com.dldata.devtools30.entity.DevtExcelTypeEntity;
import com.dldata.devtools30.repository.devtexcel.DevtExcelDao;
import com.dldata.devtools30.repository.devtexcel.DevtExcelTypeDao;
import com.dldata.devtools30.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.UUID;

/**

 */
@Controller
@RequestMapping(value = "/DevtExcelTypeController")
public class DevtExcelTypeController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    DevtExcelTypeDao devtexcelTypeDao;

    @Autowired
    DevtExcelDao devtexcelDao;

    @RequestMapping("toPage")
    public String toSysFormTypeList(){

        return "thymeleaf/devtools30/devtexcel/devtExcelType";
    }

    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Result getAllCustomType(String search){

        List<DevtExcelTypeEntity> allFormType = devtexcelTypeDao.getAllFormType();

            return Result.ok(allFormType) ;
    }

    @RequestMapping("toEdit")
    public String toSysFormTypeEdit(String id,Model model){
        List<DevtExcelTypeEntity> byTypeId = devtexcelTypeDao.findByTypeId(id);
        if(byTypeId.size()>0){
            model.addAttribute("data",byTypeId.get(0));
        }else{
            DevtExcelTypeEntity devtexcelTypeEntity= new DevtExcelTypeEntity();
            List<DevtExcelTypeEntity> allFormType = devtexcelTypeDao.getAllFormType();
            int xh = allFormType.size() + 1;
            for (int i = 0; i < allFormType.size(); i++) {
                DevtExcelTypeEntity fieldEntity = allFormType.get(i);
                xh=fieldEntity.getXh()>=xh?(fieldEntity.getXh()+1):xh;
            }
            devtexcelTypeEntity.setXh(xh);
            model.addAttribute("data",devtexcelTypeEntity);
        }
        return "thymeleaf/devtools30/devtexcel/devtExcelTypeEdit";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public Result save(DevtExcelTypeEntity devtexcelTypeEntity){

        try {
                if (devtexcelTypeEntity.getId()==null||devtexcelTypeEntity.getId().equals("")) {
                devtexcelTypeEntity.setId(UUID.randomUUID().toString());
                List<DevtExcelTypeEntity> byXh = devtexcelTypeDao.findByXh(devtexcelTypeEntity.getXh());
                if (byXh.size() > 0) {
                    return Result.error("序号已存在！");
                }
            }
            devtexcelTypeDao.save(devtexcelTypeEntity);
            return Result.ok();
        } catch (Exception ex) {
            logger.error("【执行" + this.getClass().getName() + ".save() 方法失败 】异常：" + ex.getMessage(),ex);
            return Result.error("操作错误，请稍候再试！");
        }

    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(String id,HttpServletRequest request, HttpServletResponse response) throws Exception{
        String result;

        List<DevtExcelEntity> byTypeId = devtexcelDao.findByTypeId(id);
        if(byTypeId.size()>0){

            return Result.error("分类下有表单,请删除表单后再删除分类！");
        }
        try {
            devtexcelTypeDao.deleteById(id);

            return Result.ok();
        }
        catch (Exception ex){

            logger.error("【执行"+this.getClass().getName()+".delete() 方法失败 】异常："+ex.getMessage());
            return Result.error("操作错误，请稍候再试！");
        }

    }

}
